# -*- coding: utf-8 -*-
"""
__mktime__ = '2019/6/9'
__author__ = 'Just'
__filename__ = 'comic'
"""
from flask import jsonify
from app.libs.redprint import Redprint
from app.libs.token_auth import auth
from app.model.base import db
from app.model.comic import Comic
from app.libs.error_code import DeleteSuccess, Success
from app.validators.form import ComicListForm


api = Redprint('comic')


@api.route('/<int:cid>', methods=['GET'])
@auth.login_required
def get_comic_by_id(cid):
    """获取某id漫画的基本数据"""
    comic = Comic.query.filter(cid=cid).first_or_404(msg='此漫画暂无', error_code=5000)
    comic.hide('description', 'area')
    return jsonify(comic)


@api.route('/list', methods=['POST'])
@auth.login_required
def get_comic_list():
    """获取漫画列表 根据更新排序"""
    form = ComicListForm().validate_for_api()
    comics = Comic.get_list(start=form.start.data, limit=form.limit.data)
    return jsonify(comics)


@api.route('/<int:cid>', methods=['DELETE'])
@auth.login_required
def delete_comic(cid):
    """软删除某cid漫画"""
    comic = Comic.query.filter_by(cid=cid).first_or_404(msg='此漫画暂无', error_code=5000)
    with db.auto_commit():
        comic.delete()
    return DeleteSuccess()


@api.route('/<int:cid>/renew', methods=['POST'])
@auth.login_required
def revise_comic(cid):
    """恢复已删除的某cid漫画"""
    comic = Comic.query.filter(cid=cid).first_or_404(msg='此漫画暂无', error_code=5000)
    with db.auto_commit():
        comic.status = 1
    return Success(code=201, msg='恢复成功')
